﻿using BW.EatRabbit.API.Application.Commands;
using BW.EatRabbit.Domain;
using BW.EatRabbit.ErrorCode;
using BW.EatRabbit.Infrastructure.Interfaces;
using MediatR;

namespace BW.EatRabbit.API.Application.CommandHandlers
{
    public class BasicInformationAllDeleteCommandHandler : IRequestHandler<BasicInformationAllDeleteCommand, APIResult<int>>
    {
        private readonly IBaseRepository<BasicInformation> _basicinformationRepository;
        public BasicInformationAllDeleteCommandHandler(IBaseRepository<BasicInformation> basicinformationRepository)
        {
            _basicinformationRepository = basicinformationRepository;

        }
        public Task<APIResult<int>> Handle(BasicInformationAllDeleteCommand request, CancellationToken cancellationToken)
        {
            APIResult<int> result = new APIResult<int>();
            var idlist = request.idment.Split(',').Select(x => Convert.ToInt32(x)).ToList();
            if(idlist==null)
            {
                result.Code = ResultCode.删除失败;
                result.Message = ResultCode.删除失败.ToString() + ",请选择需要删除的内容";
                return Task.FromResult(result);
            }
            var testment = _basicinformationRepository.GetAll().Where(x => idlist.Contains(x.BasicInforId));
            foreach(var item in testment)
            {
                item.IsDeleted = true;
            }
           var code= _basicinformationRepository.UpdateRange(testment);
            if(code>0)
            {
                result.Code = ResultCode.删除成功;
                result.Message = ResultCode.删除成功.ToString();
             
            }

            return Task.FromResult(result);
        }
    }
}
